﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using Biciq.Web.UI.Base;
using Biciq.Logica;
using Biciq.Datos;


namespace Biciq.Web.UI.Account
{
    public partial class Login : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }


        protected void LoginUser_LoggedIn(object sender, EventArgs e)
        {
            FormsAuthentication.GetRedirectUrl(LoginUser.UserName, false);
            String pagina = FormsAuthentication.GetRedirectUrl(LoginUser.UserName, true);
            Response.Redirect(LoginUser.DestinationPageUrl);
        }

        protected void LoginUser_Authenticate(object sender, AuthenticateEventArgs e)
        {
            SecurityService securityService = new SecurityService();
            Result resultado =  securityService.Login(LoginUser.UserName, Comun.encript(LoginUser.Password));
            if (!resultado.estado)
            {
                e.Authenticated = false;
            }
            else
            {
                var usuario = securityService.getUserByName(LoginUser.UserName);
                Session.Add("usuario", usuario);
                if (usuario.primerIngreso.Value)
                {
                    LoginUser.DestinationPageUrl = "ChangePassword.aspx";
                }
                else
                {
                    LoginUser.DestinationPageUrl = "../Principal/";
                }
                e.Authenticated = true;
            }

        }
    }
}
